﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Safe_Driving.EasyCalendar.Model;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Collections;
using Safe_Driving;

using System.Threading;
using System.Diagnostics;

using Safe_Driving.TokenUser;

namespace Safe_Driving.EasyCalendar.Controler
{
    /// <summary>
    /// 
    ///     <MyNamespace:CalendarDay/>
    ///
    /// </summary>
    public  class CalendarDay : Control
    {
        static CalendarDay()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CalendarDay), new FrameworkPropertyMetadata(typeof(CalendarDay)));

            CommandManager.RegisterClassCommandBinding(typeof(CalendarDay), new CommandBinding(AddSelectedEvent, new ExecutedRoutedEventHandler(OnExecutedAddSelectedEvent), new CanExecuteRoutedEventHandler(OnCanExecuteAddSelectedEvent)));
        }

        #region Button Add Selected Event

            public static readonly RoutedCommand AddSelectedEvent = new RoutedCommand("AddSelectedEvent", typeof(CalendarDay));

            private static void OnCanExecuteAddSelectedEvent(object sender, CanExecuteRoutedEventArgs e)
            {
                ((CalendarDay)sender).OnCanExecuteAddSelectedEvent(e);
            }


            //Test si un seul élément manquant
            private static void OnExecutedAddSelectedEvent(object sender, ExecutedRoutedEventArgs e)
            {
                //Day date
                DateTime dayDate = ((CalendarDay)sender).CurrentDate;
                
                //Vars
                ListBox lst = ((CalendarDay)sender).GetTemplateChild("lst_box") as ListBox; //The listbox
                
                List<int> list_hour = new List<int>();
                List<int> list_final = new List<int>();
            
                int index_current, last_index = 0;
                
                if (lst.SelectedItems.Count == 1) {
                    ListBoxItem item = lst.SelectedItems[0] as ListBoxItem;
                    index_current = Int32.Parse(Regex.Replace(item.Name, "H", ""));

                    list_final.Add(index_current);
                    list_final.Add(index_current+1);

                } else {
                    //On ordonne la liste des selections
                    foreach (ListBoxItem item in lst.SelectedItems)
                    {
                        if (item != null)
                        {
                            index_current = Int32.Parse(Regex.Replace(item.Name, "H", ""));
                            list_hour.Add(index_current);
                        }
                    }

                    list_hour.Sort(delegate(int i1, int i2) { return i1.CompareTo(i2); });

                    foreach (int current in list_hour)
                    {
                        if (last_index + 1 != current)
                        {
                            list_final.Add(last_index);
                            list_final.Add(current);
                        }
                        else if (current == list_hour[list_hour.Count - 1])
                        {
                            list_final.Add(last_index + 1 );
                        }
                        last_index = current;
                    }
                }

                //On test la vue qu'on a en cours et on affiche le bon panneau
                SingletonTokenUser tok = SingletonTokenUser.GetInstance();
                MainWindow mw = tok.Mw;

                if (tok.Current_tab == "indispo_Moniteur_TabItem")
                {
                    mw.manage_indispos(list_final, dayDate);
                }
                else if (tok.Current_tab == "cours_test")
                {
                    mw.manage_cours(list_final, dayDate);
                }

                lst.UnselectAll();
                e.Handled = true;
            }



            



            protected virtual void OnCanExecuteAddSelectedEvent(CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = true;
                e.Handled = false;
            }

        #endregion


        #region DayIndex

            public static readonly DependencyProperty DayIndexProperty =
                DependencyProperty.Register("DayIndex", typeof(int), typeof(CalendarDay),
                    new FrameworkPropertyMetadata(0));

            public int DayIndex
            {
                get { return (int)GetValue(DayIndexProperty); }
                set { SetValue(DayIndexProperty, value); }
            }

        #endregion

        #region CurrentDate

            public static readonly DependencyProperty CurrentDateProperty =
                DependencyProperty.Register("CurrentDate", typeof(DateTime), typeof(EasyCalendar),
                    new FrameworkPropertyMetadata((DateTime)DateTime.Now, onCurrentDateChanged));


            public DateTime CurrentDate
            {
                get { return (DateTime)GetValue(CurrentDateProperty); }
                set { SetValue(CurrentDateProperty, value); }
            }

            protected static void onCurrentDateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
            {
                ((CalendarDay)obj).onCurrentDateChanged(args);
            }

            protected virtual void onCurrentDateChanged(DependencyPropertyChangedEventArgs e)
            {
                TextBlock dayHeader = this.GetTemplateChild("dayHeader") as TextBlock;
                //dayHeader.Text = CurrentDate.DayOfWeek.ToString();
                dayHeader.Text = "toto";
            }

            private static int getIntDay(String day)
        {

            int val;
            switch (day)
            {
                case "Monday":
                    val = 1;
                    break;

                case "Tuesday":
                    val = 2;
                    break;

                case "Wednesday":
                    val = 3;
                    break;

                case "Thursday":
                    val = 4;
                    break;

                case "Friday":
                    val = 5;
                    break;

                case "Saturday":
                    val = 6;
                    break;

                default:
                    val = 7;
                    break;
            }

            return val;
        }





            public void jump_2_x_week(int nb_week)
            {
                CurrentDate -= TimeSpan.FromDays(7 * nb_week);
                TextBlock dayHeader = this.GetTemplateChild("dayHeader") as TextBlock;
                dayHeader.Text = CurrentDate.DayOfWeek.ToString() + " " + CurrentDate.Day.ToString() + "th " + getMounth(CurrentDate.Month);
            }

            public void initDates()
            {
                int todayInt = getIntDay(CurrentDate.DayOfWeek.ToString());
                int dayInt = getIntDay(this.Name);
                int interval = todayInt - dayInt;

                if (interval >= 0)
                {
                    CurrentDate = CurrentDate - TimeSpan.FromDays(interval);
                    CurrentDate = Convert.ToDateTime(CurrentDate.Day.ToString() + "/" + CurrentDate.Month.ToString() + "/" + CurrentDate.Year.ToString() + " 00:00:00");
                }
                else
                {
                    interval = 0 - interval;
                    CurrentDate = CurrentDate + TimeSpan.FromDays(interval);
                    CurrentDate = Convert.ToDateTime(CurrentDate.Day.ToString() + "/" + CurrentDate.Month.ToString() + "/" + CurrentDate.Year.ToString() + " 00:00:00");
                }

                TextBlock dayHeader = this.GetTemplateChild("dayHeader") as TextBlock;
                dayHeader.Text = CurrentDate.DayOfWeek.ToString() + " " + CurrentDate.Day.ToString() + "th " + getMounth(CurrentDate.Month);

            }

        public String getMounth(int mounth)
        {
            String mounth_str;
            switch (mounth)
            {
                case 1:
                    mounth_str = "January";
                    break;

                case 2:
                    mounth_str = "February";
                    break;

                case 3:
                    mounth_str = "March";
                    break;

                case 4:
                    mounth_str = "April";
                    break;

                case 5:
                    mounth_str = "May";
                    break;

                case 6:
                    mounth_str = "June";
                    break;

                case 7:
                    mounth_str = "July";
                    break;

                case 8:
                    mounth_str = "August";
                    break;

                case 9:
                    mounth_str = "September";
                    break;

                case 10:
                    mounth_str = "October";
                    break;

                case 11:
                    mounth_str = "November";
                    break;

                default:
                    mounth_str = "December";
                    break;
            }

            return mounth_str;
        }

        #endregion

    }
}
